package com.cmpan.networkdisk.controller.admin;

import com.cmpan.networkdisk.model.User;
import com.cmpan.networkdisk.service.AdminService;
import com.cmpan.networkdisk.utility.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * ClassName:AdminUser
 * Package:com.cmpan.networkdisk.controller.admin
 * Description:
 *
 * @Date:2020/7/10 14:43
 * @com.chuangmei
 */
@Controller
@RequestMapping("/admin")
public class AdminUserController {

    @Autowired
    private AdminService adminService;

    @PostMapping("/ediPass")
    public String editorPassword
            (String originalPass, String newPass, String question, HttpServletRequest request, Model model){

        //修改
        Integer count = adminService.UpdateUserByPasswordAndquestion(MD5.getMD5(originalPass),MD5.getMD5(newPass),question);

        if (count != null && count > 0){
            //清除Session
            request.getSession().setAttribute("user",null);
            return "index";
        }

        //重置session中的user
        model.addAttribute("user",request.getSession().getAttribute("user"));
        model.addAttribute("errorMessage","修改密码失败,因为您输入的信息有误!");
        return "/admin/modify";
    }


}
